/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cong.javadanang.controller.bean;

import cong.javadanang.model.dao.QuestionDAO;
import cong.javadanang.model.dao.impl.QuestionDAOImpl;
import cong.javadanang.model.entity.Category;
import cong.javadanang.model.entity.Question;
import cong.javadanang.model.entity.User;
import cong.javadanang.utils.Support;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

/**
 *
 * @author Pham
 */
@ManagedBean
@RequestScoped
public class QuestionManagement {
    
    private final QuestionDAO QUESTION_SERVICE = QuestionDAOImpl.getInstance();
    
    private List<Question> questions = null;
    private Question question;    

    public QuestionManagement() {
        questions = QUESTION_SERVICE.getQuestions(2);
    }
    
    public void createQuestion(ActionEvent evt) {
        String msg = "";
        String questionName = this.question.getQuestionName();
        String questionDetail = this.question.getQuestionDetail();
        Date createDate = new Date();
        User user = Support.getCurrentUser();
        Category category = this.question.getCategory();
        boolean active = true;
        
        Question newQuestion = new Question(Integer.MIN_VALUE, category, user, questionName, questionDetail, createDate, createDate, 0, 1, active);
        if (user != null) {
            if (!QUESTION_SERVICE.checkExistQuestion(newQuestion.getQuestionName())) {
                if (QUESTION_SERVICE.createQuestion(newQuestion)) {
                    msg += "Bạn đã tạo câu hỏi thành công.";
                } else {
                    msg += "Tạo câu hỏi mới thất bạn! Vui lòng kiểm tra lại thông tin.";
                }
            }else{
                msg += "Câu hỏi này đã tồn tại, vui lòng chọn câu hỏi khác.";
            }
        } else {
            msg += "Vui lòng đăng nhập để đặt câu hỏi?";
        }
        addMessages(msg);
    }
    
    public void addVote(ActionEvent event) {
        int questionId = this.question.getQuestionId();
        int vote = QUESTION_SERVICE.getQuestionById(questionId).getVote() + 1;
        Question currentQuestion = new Question(questionId, null, null, null, null, null, null, vote, vote, true);
        if (!QUESTION_SERVICE.updateVote(currentQuestion)) {
            addMessages("Có lỗi xảy ra khi bình chọn câu hỏi này.");
        }
    }
    
    public void subVote(ActionEvent event) {
        int questionId = this.question.getQuestionId();
        int vote = QUESTION_SERVICE.getQuestionById(questionId).getVote() - 1;
        Question currentQuestion = new Question(questionId, null, null, null, null, null, null, vote, vote, true);
        if (!QUESTION_SERVICE.updateVote(currentQuestion)) {
            addMessages("Có lỗi xảy ra khi bình chọn câu hỏi này.");
        }
    }
    
    public void addViewer(int questionId) {
//        int questionId = this.question.getQuestionId();
        int viewCount = QUESTION_SERVICE.getQuestionById(questionId).getViewCount() + 1;
        Question currentQuestion = new Question(questionId, null, null, null, null, null, null, viewCount, viewCount, true);
        if (!QUESTION_SERVICE.updateViewCount(currentQuestion)) {
            addMessages("Có lỗi xảy ra khi bình chọn câu hỏi này.");
        }
    }
    
    public void addMessages(String msg) {
        FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, "Message!");
        FacesContext.getCurrentInstance()
                .addMessage(null, fm);
    }
    
    public List<Question> getQuestions() {
        return questions;
    }
    
    public void setQuestions(List<Question> questions) {
        this.questions = questions;
    }
    
    public Question getQuestion() {
        if (question == null) {
            question = new Question();
        }
        return question;
    }
    
    public void setQuestion(Question question) {
        this.question = question;
    }    
    
}
